//
//  ISWProgressHUD
//  GQHotel
//
//  Created by cienet on 14-5-13.
//  Copyright (c) 2014年 cienet. All rights reserved.
//

#import "CLProgressHUD.h"
#import "AppDelegate.h"

#define kcAlertViewBgColor      CLRgbaColor(17.0f, 46.0f, 86.0f,0.9f)
#define HUDMaxInvaliedTime      60
#define HUDMessageInvaliedTime  1.8

@implementation CLProgressHUD

+ (CLProgressHUD *)sharedInstance {
  AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  for (UIView *tmpview in [delegate.window subviews]) {
    if ([tmpview isKindOfClass:[CLProgressHUD class]]) {
      [tmpview removeFromSuperview];
    }
  }
  CLProgressHUD *hudAlert = [[CLProgressHUD alloc] initWithView:delegate.window];
    hudAlert.dimBackground = YES;
  [delegate.window addSubview:hudAlert];
  [hudAlert show:YES];

  return hudAlert;
}

+ (CLProgressHUD *)sharedInstanceWithView:(UIView *)view {
  CLProgressHUD *hudAlert = [[CLProgressHUD alloc] initWithView:view];
    hudAlert.dimBackground = YES;

  [view addSubview:hudAlert];
  [hudAlert show:YES];

  return hudAlert;
}

+ (void)showMessage:(NSString *)message {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstance];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.detailsLabelText = message;
    hudAlert.detailsLabelFont = hudAlert.labelFont;
    hudAlert.yOffset = 150.0f;
    hudAlert.removeFromSuperViewOnHide = YES;
    hudAlert.alpha = 0.75;
    [hudAlert hide:YES afterDelay:HUDMessageInvaliedTime];
  });
}

+ (void)showMessage:(NSString *)message withView:(UIView *)view {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstanceWithView:view];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.detailsLabelText = message;
    hudAlert.detailsLabelFont = hudAlert.labelFont;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kcAlertViewBgColor];
    [hudAlert hide:YES afterDelay:HUDMessageInvaliedTime];
  });
}

+ (void)showMessage:(NSString *)message afterDelay:(NSInteger)time {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstance];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.detailsLabelText = message;
    hudAlert.detailsLabelFont = hudAlert.labelFont;
    hudAlert.yOffset = 150.0f;
    hudAlert.removeFromSuperViewOnHide = YES;
    hudAlert.alpha = 0.75;
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showMessage:(NSString *)message withView:(UIView *)view afterDelay:(NSInteger)time {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstanceWithView:view];

    hudAlert.mode = MBProgressHUDModeText;
    hudAlert.labelText = message;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kcAlertViewBgColor];
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showMessage:(NSString *)message andImage:(NSString*)imageName afterDelay:(NSInteger)time {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstance];

    hudAlert.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hudAlert.mode = MBProgressHUDModeCustomView;
    hudAlert.labelText = message;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kcAlertViewBgColor];
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showMessage:(NSString *)message
           withView:(UIView *)view
           andImage:(NSString*)imageName
         afterDelay:(NSInteger)time {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstanceWithView:view];

    hudAlert.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hudAlert.mode = MBProgressHUDModeCustomView;
    hudAlert.labelText = message;
    hudAlert.yOffset = 0.f;
    hudAlert.removeFromSuperViewOnHide = YES;
    [hudAlert setColor:kcAlertViewBgColor];
    [hudAlert hide:YES afterDelay:time];
  });
}

+ (void)showWaitAlertWithMessage:(NSString *)text {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstance];
    hudAlert.labelText = text;
    hudAlert.removeFromSuperViewOnHide = YES;
  });
}

+ (void)showWaitAlertWithMessage:(NSString *)text withView:(UIView *)view {
  CLGcdMain(^{
    CLProgressHUD *hudAlert = [CLProgressHUD sharedInstanceWithView:view];
    hudAlert.labelText = text;
    hudAlert.removeFromSuperViewOnHide = YES;
  });
}

+ (void)showWaitAlert {
  CLGcdMain(^{
    [CLProgressHUD sharedInstance];
  });
}

+ (void)showWaitAlertWithView:(UIView *)view {
  CLGcdMain(^{
    [CLProgressHUD sharedInstanceWithView:view];
  });
}

+ (void)hideProgressHUD {
  CLGcdMain(^{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    for (UIView *tmpview in [delegate.window subviews]) {
      if ([tmpview isKindOfClass:[CLProgressHUD class]]) {
        [tmpview removeFromSuperview];
        [(CLProgressHUD *)tmpview hide:YES];
      }
    }
  });
}

+ (void)hideProgressHUDWithView:(UIView *)view {
  CLGcdMain(^{
    for (UIView *tmpview in [view subviews]) {
      if ([tmpview isKindOfClass:[CLProgressHUD class]]) {
        [tmpview removeFromSuperview];
        [(CLProgressHUD *)tmpview hide:YES];
      }
    }
  });
}

+ (void)showSelectAlertView:(NSString *)title actionBlock:(CustomIOS7AlertViewBlock)block {
  CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
  CGFloat viewWidth = 290.0f;
  CGFloat viewHeight= 100.0f;
  CGFloat hDistance = 20.0f;
  CGFloat vDistance = 10.0f;

  UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight)];

  CGFloat lableWidth = CLViewWidth(containView)-2*hDistance;
  CGFloat lableHeight= CLViewHeight(containView)-2*vDistance;
  UILabel *textLable =
        [[UILabel alloc] initWithFrame:CGRectMake(hDistance, vDistance, lableWidth, lableHeight)];
  [textLable setText:title];
  [textLable setBackgroundColor:[UIColor clearColor]];
  [textLable setFont:[UIFont fontWithName:@"MyriadPro-Semibold" size:12.0f]];
  [textLable setTextAlignment:NSTextAlignmentCenter];
  [textLable setTextColor:[UIColor blackColor]];
  [textLable setNumberOfLines:0];
  [containView addSubview:textLable];

  [alertView setContainerView:containView];
  [alertView setButtonTitles:[NSMutableArray arrayWithObjects:NSLocalizedString(@"Global_Dialog_OK", @"确定"),
                                                              NSLocalizedString(@"Global_Cancel", @"取消"),
                                                              nil]];
  [alertView setOnButtonTouchUpInside:block];

  [alertView setUseMotionEffects:true];
  [alertView show];
}


@end
